package com.agrm.rabc.controller;


import com.agrm.model.common.dtos.ResponseResult;
import com.agrm.model.rabc.dto.GroupRoleMapping;
import com.agrm.rabc.service.GroupRoleMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/group_roles", produces = MediaType.APPLICATION_JSON_VALUE)
public class GroupRoleMappingApi {
    private GroupRoleMappingService groupRoleMappingService;

    @Autowired
    public void setGroupRoleMappingService(GroupRoleMappingService groupRoleMappingService) {
        this.groupRoleMappingService = groupRoleMappingService;
    }

    @GetMapping
    public ResponseResult findAll(GroupRoleMapping grm) {
        List<GroupRoleMapping> mappings = groupRoleMappingService.findAll(grm);
        return ResponseResult.okResult(mappings);
    }

    @GetMapping("/id/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id) {
        GroupRoleMapping grm = groupRoleMappingService.findById(id);
        if (grm == null) {
            return ResponseResult.errorResult(-1,"未查询到指定编号的组-角色关联");
        } else {
            return ResponseResult.okResult(grm);
        }
    }

    @PostMapping
    public ResponseResult save(@RequestBody GroupRoleMapping grm) {
        boolean success = groupRoleMappingService.save(grm);
        if (success) {
            return ResponseResult.okResult(200,"保存成功！");
        } else {
            return ResponseResult.errorResult(-1,"保存组-角色关联失败");
        }
    }

    @PutMapping
    public ResponseResult update(@RequestBody GroupRoleMapping grm) {
        boolean success = groupRoleMappingService.update(grm);
        if (success) {
            return ResponseResult.okResult(200,"更新成功！");
        } else {
            return ResponseResult.errorResult(-1,"修改组-角色关联失败");
        }
    }

    @DeleteMapping
    public ResponseResult deleteByIds(@RequestBody Integer[] ids) {
        int count = groupRoleMappingService.deleteByIds(List.of(ids));
        if (count > 0) {
            return ResponseResult.okResult(count);
        } else {
            return ResponseResult.errorResult(-1,"删除组-角色关联失败");
        }
    }
}
